package com.team12.hotel404.controller.common;


import com.team12.hotel404.domain.Comment;
import com.team12.hotel404.service.CommentService;
import com.team12.hotel404.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/comment")

public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;

    @RequestMapping("/review")
    @ResponseBody
    public void review( @RequestParam("reviewContent") String content, @RequestParam("hotelId")Integer hotelId, HttpSession session ){
        Integer userId = (Integer) session.getAttribute("current_userid");
        Comment comment = new Comment();
        comment.setuId(userId);
        comment.sethId(hotelId);
        comment.setCommentText(content);
        commentService.commentAdd(comment);

    }
    @RequestMapping("/showReview")
    @ResponseBody
    public Object showReview(@RequestParam("hotelId") Integer hotelId,Model model){
        List<Comment> comments=commentService.listByHotelId(hotelId);
        model.addAttribute("comments",comments);
        List<Object> objectList = new ArrayList<Object>();
        List<Object> objectList1 = new ArrayList<Object>();
        List<Object> objectList2 = new ArrayList<Object>();
        for (int i=0;i<comments.size();i++) {
            objectList2.add(comments.get(i).getUserName());
            objectList1.add(comments.get(i).getCommentText());
        }
        objectList.add(objectList1);
        objectList.add(objectList2);
        return objectList;
    }



    }


